<?php

class Contact extends MX_Controller {

    function __construct() {
        parent::__construct();
    }

    
    function index() {
        require_once('recaptchalib.php');
        $publickey = "6Lf6rtkSAAAAAMwRxACvXN3mmFOmTVuGt2nZUwmw";
        $privatekey = "6Lf6rtkSAAAAANTo81yYhkoQnxFAAmP53yh6t7Qz";        
        $resp = null;        
        $error = null;
        if ($this->input->post('recaptcha_response_field')) {
            $resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

            if ($resp->is_valid) {
                $content = $this->input->post('txtContent');
                $name = $this->input->post('txtName');
                $email = $this->input->post('txtEmail');
                
                $user = new User();
                $user->where('user_activation_key','Admin')
                        ->where('user_status',1)
                        ->get();
                $email_to = $user->user_email;
                
                //sendmail
                $option = new Option();
                $option->where('option_name','mail_server_host')->get();
                $host = $option->option_value; 
                
                $option = new Option();
                $option->where('option_name','mail_server_port')->get();
                $port = $option->option_value;  
                
                $option = new Option();
                $option->where('option_name','mail_server_username')->get();
                $usermail = $option->option_value;  
                
                $option = new Option();
                $option->where('option_name','mail_server_password')->get();
                $passmail = $option->option_value;                
               
               // cấu hình gửi mail 
                $config['protocol']='smtp';
                $config['smtp_host']=$host;
                $config['smtp_port']=$port;
                $config['smtp_timeout']='30';
                $config['smtp_user']=$usermail;
                $config['smtp_pass']=$passmail;
                $config['charset']='utf-8';
                $config['newline']="\r\n";
                $config['mailtype'] = 'html';
                $this->email->initialize($config);
                
                $this->email->from($email,'Ý kiến đóng góp'); 
                $this->email->to($email_to);                                             
                $this->email->subject('Ý kiến đóng góp');
                $email_msg='Nội dung đóng góp: '.$content.'<br/>';
                $email_msg.='Từ: '.$name.'<br/>';
                $email_msg.='Email: '.$email.'<br/>';
                $this->email->message($email_msg);  
                $this->email->send();
                
                redirect('contact/thanks');
            } else {               
                $error = $resp->error;
            }
        }        
        $data['captcha'] = recaptcha_get_html($publickey, $error);
        $data['message'] = '';
        $this->common->load_template('contact_index', $data);
    }
    
    function thanks()
    {
        $data['message'] = '';
        $this->common->load_template('contact_thanks', $data);
    }
}

?>
